/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.com.sml.SEFM.DAO;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.persistence.RollbackException;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.SystemException;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import vn.com.sml.SEFM.domain.Cities;
import vn.com.sml.SEFM.domain.Countries;
import vn.com.sml.SEFM.domain.Ip;
import vn.com.sml.SEFM.util.HibernateUtil;
/**
 *
 * @author Tunp
 */
public class IPDAO {
    
    public static List<Ip> listIps() throws RollbackException, HeuristicMixedException, HeuristicRollbackException, SecurityException, IllegalStateException, SystemException {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        List<Ip> lst = new ArrayList<Ip>();
        try {
            transaction = (Transaction) session.beginTransaction();
            
            List lstIp = session.createQuery("from Ip").list();
            for (Iterator iterator = (Iterator) lstIp.iterator();iterator.hasNext();)
            {
                Ip ip = (Ip) iterator.next();
                Cities cities = (Cities) session.createQuery("select i.cities from Ip as i where i.ipId="+ip.getIpId()).uniqueResult();
                Countries ctr = (Countries) session.createQuery("select i.countries from Cities as i where i.cityId="+cities.getCityId()).uniqueResult();
                cities.setCountries(ctr);
                ip.setCities(cities);
                lst.add(ip);
            }
            transaction.commit();
        }
        catch (HibernateException e){
            transaction.rollback();
            e.printStackTrace();
        }
        finally {
            session.close();
        }
        return lst;
    }

    public static Ip getIpById(int id) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        Ip Ip = null;
        Cities Cities = null;

        try {
            transaction = (Transaction) session.beginTransaction();
            
            Ip = (Ip) session.createQuery("from Ip where ipId="+id).uniqueResult();
            Cities = (Cities) session.createQuery("select i.cities from Ip as i where i.ipId="+id).uniqueResult();

            Ip.setCities(Cities);
            
            transaction.commit();
        }
        catch (HibernateException e){
            transaction.rollback();
            e.printStackTrace();
        }
        finally {
            session.close();
        }
        return Ip;
    }
    
    public static Countries getIpCountriesById(int id) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        Countries ctr = null;

        try {
            transaction = (Transaction) session.beginTransaction();
            
            ctr = (Countries) session.createQuery("select i.cities.countries from Ip as i where ipId="+id).uniqueResult();            
            transaction.commit();
        }
        catch (HibernateException e){
            transaction.rollback();
            e.printStackTrace();
        }
        finally {
            session.close();
        }
        return ctr;
    }

    public static List<Ip> listIps(String search_col, String search_text) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        List<Ip> lst = new ArrayList<Ip>();
        if(search_col.equals("IP")) {
            search_col = "ip";
        }
        if(search_col.equals("city")) {
            search_col = "cities.cityName";
        }
        if(search_col.equals("country")) {
            search_col = "cities.countries.countryName";
        }
        try {
            transaction = (Transaction) session.beginTransaction();
            
            List lstIp = session.createQuery("from Ip where " + search_col + " like '%" + search_text + "%'").list();
            for (Iterator iterator = (Iterator) lstIp.iterator();iterator.hasNext();)
            {
                Ip ip = (Ip) iterator.next();
                Cities cities = (Cities) session.createQuery("select i.cities from Ip as i where i.ipId="+ip.getIpId()).uniqueResult();
                Countries ctr = (Countries) session.createQuery("select i.countries from Cities as i where i.cityId="+cities.getCityId()).uniqueResult();
                cities.setCountries(ctr);
                ip.setCities(cities);
                lst.add(ip);
            }
            transaction.commit();
        }
        catch (HibernateException e){
            transaction.rollback();
            e.printStackTrace();
        }
        finally {
            session.close();
        }
        return lst;
    }

    public static Integer saveIP(Ip ip) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        Integer ipId = null;
        try {
            transaction = (Transaction) session.beginTransaction();
            
            ipId = (Integer) session.save(ip);
            transaction.commit();
        }
        catch (HibernateException e){
            transaction.rollback();
            e.printStackTrace();
        }
        finally {
            session.close();
        }
        return ipId;
    }
}